package com.avic.rong.wedgit.customView;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.bean.product.SoftwareBean;
import com.avic.rong.goodsSort.api.SoftwareAttrsApi;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.DateUtils;
import com.avic.rong.utils.StringUtils;
import com.avic.rong.utils.ToastTools;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Creater: Created by wangyz on 2019/9/6.
 * Email: 936804097@qq.com
 */
public class SoftwareAttrFragment extends Fragment {


    private Context mContext;
    @BindView(R.id.recycler_list_attrs)
    RecyclerView recycler_list_attrs;//文本基本属性显示

    public List<ItemSearchBean> itemSearchBeanList = new ArrayList<>();

    private SearchGroupAdapter searchGroupAdapter;
    private int catIdSelected;

    public static SoftwareAttrFragment getInstance(Context context, String catId) {
        SoftwareAttrFragment fragment = new SoftwareAttrFragment();
        fragment.mContext = context;
        if (!StringUtils.isEmpty(catId)) {
            fragment.catIdSelected = Integer.parseInt(catId);
        }
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.drawer_standard, container, false);
        ButterKnife.bind(this, view);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        searchGroupAdapter = new SearchGroupAdapter(mContext);
        recycler_list_attrs.setLayoutManager(linearLayoutManager);
        recycler_list_attrs.setAdapter(searchGroupAdapter);
        SoftwareAttrsApi softwareAttrsApi = new SoftwareAttrsApi((BaseActivity) mContext, softwareAttrsListener);
        HttpManager.getInstance().doHttpDeal(softwareAttrsApi);

        return view;
    }


    @OnClick({R.id.tv_clear, R.id.tv_ok})
    public void clickOperation(View view) {
        int viewId = view.getId();
        switch (viewId) {
            case R.id.tv_ok://确定
                String attrPro = "";
                for (ItemSearchBean searchBean : itemSearchBeanList) {
                    if (SearchGroupAdapter.TYPE_SELECT == searchBean.getControlType()) {
                        if (!StringUtils.isEmpty(searchBean.getValue())) {
                            if (searchBean.getId() == 50000) {//软件工具分类
                                attrPro = attrPro + "&" + searchBean.getId() + "++" + searchBean.getIdValue() + "__" + searchBean.getValue();
                            } else {
                                attrPro = attrPro + "&" + searchBean.getId() + "__" + searchBean.getValue() + "__" + searchBean.getAttrType();
                            }
                        }
                    } else if (SearchGroupAdapter.TYPE_TIME == searchBean.getControlType()) {
                        String stardTime = "";
                        String endTime = "";
                        if (!StringUtils.isEmpty(searchBean.getValue())) {
                            endTime = DateUtils.getFormatShort(System.currentTimeMillis());
                            if (ConstantsUtils.TIME_ONE_MONTH.equalsIgnoreCase(searchBean.getValue())) {
                                stardTime = DateUtils.getBeforeDateShort(30);
                            } else if (ConstantsUtils.TIME_THREE_MONTH.equalsIgnoreCase(searchBean.getValue())) {
                                stardTime = DateUtils.getBeforeDateShort(30 * 3);
                            } else if (ConstantsUtils.TIME_HALF_YEAR.equalsIgnoreCase(searchBean.getValue())) {
                                stardTime = DateUtils.getBeforeDateShort(30 * 6);
                            } else if (ConstantsUtils.TIME_ONE_YEAR.equalsIgnoreCase(searchBean.getValue())) {
                                stardTime = DateUtils.getBeforeDateShort(365);
                            }
                        } else {
                            stardTime = searchBean.getStartTime();
                            endTime = searchBean.getEndTime();
                        }
                        if (!StringUtils.isEmpty(stardTime)) {
                            attrPro = attrPro + "&start" + searchBean.getId() + "__" + stardTime;
                        }
                        if (!StringUtils.isEmpty(endTime)) {
                            attrPro = attrPro + "&end" + searchBean.getId() + "__" + endTime;
                        }
                    } else {
                        if (!StringUtils.isEmpty(searchBean.getValue())) {
                            attrPro = attrPro + "&" + searchBean.getId() + "__" + searchBean.getValue() + "__" + searchBean.getAttrType();
                        }
                    }
                }
                Logger.i("-------------------------------" + attrPro);
                if (attrPro.length() > 0) {
                    attrPro = attrPro.substring(1);
                }
                Logger.i("--------------------------ff-----" + attrPro);
                if (clickInterface != null) {
                    clickInterface.clickOK(attrPro);
                }
                break;
            case R.id.tv_clear://清空
                for (ItemSearchBean searchBean : itemSearchBeanList) {
                    for (ItemSearchBean.SubBean subBean : searchBean.getSubBeanList()) {
                        subBean.setChecked(false);
                    }
                    searchBean.setValue("");
                }
                searchGroupAdapter.clearData();
                break;
        }

    }


    //获取标准数据监听
    HttpOnNextListener<BaseResultEntity> softwareAttrsListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!ConstantsUtils.STATUS_SUCCESS_200.equals(subjects.getCode())) {
                ToastTools.showToast(mContext, subjects.getMsg());
                return;
            }
            SoftwareBean softwareBean = (SoftwareBean) subjects.getData();

            //子分类添加
            for (SoftwareBean.CategoryBean categoryBean : softwareBean.getCategory()) {
                if (categoryBean.getCatId() == 456) {
                    ItemSearchBean itemSearchBean = new ItemSearchBean();
                    for (SoftwareBean.CategoryBean tempBean : categoryBean.getSubCategory()) {
                        ItemSearchBean.SubBean subBean = new ItemSearchBean.SubBean();
                        subBean.setId(tempBean.getCatId());
                        subBean.setName(tempBean.getCatName());
                        if (catIdSelected == tempBean.getCatId()) {
                            subBean.setChecked(true);
                            itemSearchBean.setIdValue(String.valueOf(tempBean.getCatId()));
                            itemSearchBean.setValue(tempBean.getCatName());
                        }
                        itemSearchBean.getSubBeanList().add(subBean);
                    }
                    itemSearchBean.setId(50000);
                    itemSearchBean.setName("产品种类");
                    itemSearchBean.setShowTimeGroupLayout(false);
                    itemSearchBean.setControlType(SearchGroupAdapter.TYPE_SELECT);
                    itemSearchBeanList.add(itemSearchBean);
                }
            }

            for (SoftwareBean.AttrBean attrBean : softwareBean.getAllAttr()) {
                ItemSearchBean itemSearchBean = new ItemSearchBean();
                for (String value : attrBean.getAttrValues()) {
                    if (!StringUtils.isEmpty(value)) {
                        ItemSearchBean.SubBean subBean = new ItemSearchBean.SubBean();
                        subBean.setName(value);
                        subBean.setId(attrBean.getId());
                        itemSearchBean.getSubBeanList().add(subBean);
                    }
                }
                itemSearchBean.setAttrType(attrBean.getAttrType());
                itemSearchBean.setName(attrBean.getAttrName());
                itemSearchBean.setShowTimeGroupLayout(false);
                itemSearchBean.setControlType(SearchGroupAdapter.TYPE_SELECT);
                if (attrBean.getId() == 32){//平台载体
                    itemSearchBean.setSpanCount(1);
                    itemSearchBean.setMaxCountFold(3);
                }

                itemSearchBean.setId(attrBean.getId());
                itemSearchBeanList.add(itemSearchBean);
            }

            searchGroupAdapter.setDataList(itemSearchBeanList);
            searchGroupAdapter.notifyDataSetChanged();
        }

        @Override
        public void onError(final Throwable e) {
            ToastTools.showToast(mContext, e.getMessage());
        }
    };

    private ClickInterface clickInterface;

    public void setClickInterface(ClickInterface clickInterface) {
        this.clickInterface = clickInterface;
    }


    public interface ClickInterface {
        public void clickOK(String attrPro);
    }

}
